package com.example.vv.model.setupTreeListDetails;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toolbar;

import com.example.vv.R;
import com.example.vv.fragmentWork.entity.ArticleListEntity;
import com.example.vv.fragmentWork.mvp.MvpActivity;
import com.example.vv.model.setupTreeListDetails.contract.STLDContract;
import com.example.vv.model.setupTreeListDetails.presenter.STLDPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class STLDActivity extends MvpActivity<STLDContract.View, STLDPresenter> implements STLDContract.View {

    @BindView(R.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.stld_web)
    WebView stldWeb;

    @Override
    public int initLayout() {
        return R.layout.activity_s_t_l_d;
    }

    @Override
    public STLDPresenter initPresenter() {
        return new STLDPresenter();
    }

    @SuppressLint("NewApi")
    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        titleToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleView() {
        return null;
    }


    @Override
    public Context getMContext() {
        return getMContext();
    }

   @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void getData(ArticleListEntity.DatasBean bean){
       String link = bean.getLink();
       titleContent.setText(bean.getChapterName());
       stldWeb.loadUrl(link);
       initWeb();

   }
    public void initWeb() {
        stldWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        stldWeb.setLayerType(View.LAYER_TYPE_HARDWARE,null);
        WebSettings settings = stldWeb.getSettings();
        settings.setSupportMultipleWindows(true);
        settings.setUseWideViewPort(true);
        settings.setAllowFileAccess(true);
        settings.setSupportZoom(false);
        settings.setDisplayZoomControls(false);
        settings.setBuiltInZoomControls(false);
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}